package com.zpy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpy.pojo.Book;
import com.zpy.pojo.Circulate;
import com.zpy.pojo.User;
import com.zpy.service.BookService;
import com.zpy.service.CirculateService;
import com.zpy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("circulate")
public class CirculateController {
    @Autowired
    private CirculateService circulateService;
    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;
    //查询其他单位并且图书状态为可借阅的图书
    @RequestMapping("listBook")
    public String listBook(@RequestParam(required = false,value ="pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(required = false,value ="pageSize",defaultValue = "10")Integer pageSize,
                           Model model, Book book, HttpSession session){

        if (pageNum<0 || pageNum.equals("") || pageNum==null){
            pageNum=1;
        }
        if (pageSize<0 || pageSize.equals("") || pageSize==null){
            pageSize=10;
        }
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Book> qw=new QueryWrapper<>();
        if (book.getBname()!=null){
            qw.like("bname",book.getBname());
        }
        String unit = (String) session.getAttribute("unit");
        qw.notIn("unit",unit);//必须是其他单位的
        qw.orderByAsc("status");
        qw.eq("isopen",1);//必须是开放的
        List<Book> list = bookService.list(qw);
        PageInfo<Book> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        return "worker-otherunit-book-list";

    }

    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id,Model model){
        Book byId = bookService.getById(id);
        model.addAttribute("book",byId);
        return "worker-book-detail";
    }

    @RequestMapping("preSaveCirculate/{bookId}")
    public String preSaveCirculate(@PathVariable Integer bookId,Model model, HttpSession session){
        Book byId = bookService.getById(bookId);
        model.addAttribute("book",byId);
        Integer userId = (Integer) session.getAttribute("userId");
        String username = (String) session.getAttribute("currentUser");
        String unit = (String) session.getAttribute("unit");
        model.addAttribute("username",username);
        model.addAttribute("userId",userId);
        model.addAttribute("unit",unit);
        Circulate circulate=new Circulate();
        return "worker-circulate-save";
    }

    //添加流通申请
    @RequestMapping("saveCirculate")
    public String saveCirculate(Circulate circulate){
        Book book = bookService.getById(circulate.getBid());
        book.setStatus(2);//申请中
        bookService.updateById(book);
        boolean save = circulateService.save(circulate);
        return "redirect:/circulate/listInput";
    }

    //本单位流入的申请
    @RequestMapping("listInput")
    public String listInput(HttpSession session,@RequestParam(required = false,value ="pageNum",defaultValue = "1")Integer pageNum,
                            @RequestParam(required = false,value ="pageSize",defaultValue = "10")Integer pageSize,Circulate circulate,
                            Model model){
        if (pageNum<0 || pageNum.equals("") || pageNum==null){
            pageNum=1;
        }
        if (pageSize<0 || pageSize.equals("") || pageSize==null){
            pageSize=10;
        }
        PageHelper.startPage(pageNum,pageSize);
        String unit = (String) session.getAttribute("unit");//当前登录的单位
        QueryWrapper<User>qw=new QueryWrapper<>();
        qw.eq("unit",unit);
        List<User> list = userService.list(qw);
        List<Integer>idList=new ArrayList<>();
        for (User user : list) {
            idList.add(user.getId());
        }
        QueryWrapper<Circulate>queryWrapper=new QueryWrapper<>();
        queryWrapper.in("uid",idList);
        if (circulate.getStatus()!=null){
            queryWrapper.eq("status",circulate.getStatus());
        }
        List<Circulate> list1 = circulateService.list(queryWrapper);
        PageInfo<Circulate>pageInfo=new PageInfo<>(list1);
        model.addAttribute("pageInfo",pageInfo);
        return "worker-circulate-input";
    }

    //归还流通申请（工作人员）
    @RequestMapping("returnCirculate/{id}")
    public String returnCirculate(@PathVariable Integer id){
        Circulate circulate = circulateService.getById(id);
        //修改流通表状态(变成已完成)
        circulate.setStatus(3);//表示归还中
        circulateService.updateById(circulate);
        return "redirect:/circulate/listInput";


    }

    //本单位流出的申请
    @RequestMapping("listOutput")
    public String listOutput(HttpSession session,@RequestParam(required = false,value ="pageNum",defaultValue = "1")Integer pageNum,
                            @RequestParam(required = false,value ="pageSize",defaultValue = "10")Integer pageSize,Circulate circulate,
                            Model model){
        if (pageNum<0 || pageNum.equals("") || pageNum==null){
            pageNum=1;
        }
        if (pageSize<0 || pageSize.equals("") || pageSize==null){
            pageSize=10;
        }
        PageHelper.startPage(pageNum,pageSize);
        String unit = (String) session.getAttribute("unit");//当前登录的单位
        QueryWrapper<Circulate>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("bunit",unit);
        if (circulate.getStatus()!=null){
            queryWrapper.eq("status",circulate.getStatus());
        }
        List<Circulate> list1 = circulateService.list(queryWrapper);
        PageInfo<Circulate>pageInfo=new PageInfo<>(list1);
        model.addAttribute("pageInfo",pageInfo);
        return "worker-circulate-output";
    }


    //同意流出
    @RequestMapping("agree/{id}")
    public String agree(@PathVariable Integer id){
        Circulate circulate = circulateService.getById(id);
        circulate.setCirculateTime(new Date());
        circulate.setStatus(1);
        circulateService.updateById(circulate);
        //修改图书的状态为再借中
        Book book = bookService.getById(circulate.getBid());
        book.setStatus(1);
        bookService.updateById(book);
        return "redirect:/circulate/listOutput";
    }

    //拒绝流出
    @RequestMapping("refuse/{id}")
    public String refuse(@PathVariable Integer id){
        Circulate circulate = circulateService.getById(id);
        circulate.setStatus(2);//状态为已完成
        circulateService.updateById(circulate);
        return "redirect:/circulate/listOutput";
    }

    //确认已返还
    @RequestMapping("confirmReturn/{id}")
    public String confirmReturn(@PathVariable Integer id){
        Circulate circulate = circulateService.getById(id);
        //设置归还时间
        circulate.setReturnTime(new Date());
        circulate.setStatus(2);//修改状态为已完成
        circulateService.updateById(circulate);
        //修改图书的状态为可借阅
        Book book = bookService.getById(circulate.getBid());
        book.setStatus(0);
        bookService.updateById(book);
        return "redirect:/circulate/listOutput";
    }

}
